package com.tool.btn;

import com.tool.Document.RichDocument;
import com.tool.PanelDisplay.PetPanelShow;
import com.tool.image.ImageMixDeal;
import com.tool.image.ManimgAttribute;
import com.tool.imagemonitor.FightingMonitor;
import com.tool.role.RoleData;
import come.tool.Fighting.FightingMixDeal;
import come.tool.handle.HandleState;
import org.apache.commons.lang.StringUtils;
import org.come.Frame.*;
import org.come.Jpanel.TestPetJpanel;
import org.come.Jpanel.ZhuJpanel;
import org.come.bean.FightOperation;
import org.come.bean.LoginResult;
import org.come.bean.NChatBean;
import org.come.entity.Baby;
import org.come.entity.Goodstable;
import org.come.entity.RoleSummoning;
import org.come.mouslisten.*;
import org.come.shortcut.XBjczJpanelxsjm;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;
import org.skill.frame.SkillMainFrame;
import tab.ButtonTabComponent;
import tab.Main;
import tab.TabJFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MyAWTEventListener implements AWTEventListener {
	private Main mains;
	private TabJFrame tabJFrame;
	ButtonTabComponent buttonTabComponent;
	private static String mesageType = "当前";// 消息类型
	private static List<String> msgOldList = new ArrayList<>();//历史消息存储
	private static int msgnum = 0;//
	@Override
	public void eventDispatched(AWTEvent event) {
		if (event.getClass() == KeyEvent.class) {
			// 被处理的事件是键盘事件.
			KeyEvent keyEvent = (KeyEvent) event;
			if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
				keyReleased(keyEvent);
			}
		}
	}
	Graphics g;
	int s = 0;
	private void keyReleased(KeyEvent event) {
		//快捷切换
		if (event.isControlDown() && event.getKeyCode() == KeyEvent.VK_TAB) {
			System.out.println("switch=" + org.come.test.Main.index);
			return;
		}
		if (event.getKeyCode() == KeyEvent.VK_3) {
			if(TabJFrame.index-1<=s) {
				s = 0;
			}else {
				s = s+1;
			}
		}
		if (event.getKeyCode() == KeyEvent.VK_ENTER) {//回车发送消息
			Mesage();
		}else if(event.getKeyCode() == KeyEvent.VK_F1){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F1);
		} else if(event.getKeyCode() == KeyEvent.VK_F2){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F2);
		}else if(event.getKeyCode() == KeyEvent.VK_F3){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F3);
		}else if(event.getKeyCode() == KeyEvent.VK_F4){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F4);
		}else if(event.getKeyCode() == KeyEvent.VK_F5){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F5);
		}else if(event.getKeyCode() == KeyEvent.VK_F6){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F6);
		}else if(event.getKeyCode() == KeyEvent.VK_F7){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F7);
		}else if(event.getKeyCode() == KeyEvent.VK_F8){
			HotKeyMouseListen.doHotKey(HotKeyMouseListen.F8);
		} else if (event.getKeyCode() == KeyEvent.VK_UP) {//上 发送历史内容
			if(msgnum>0) {
				msgnum = msgnum-1;
			}
			if(msgnum>=0) {
				ZhuFrame.getZhuJpanel().getSendMes().setText(msgOldList.get(msgnum));
			}
		} else if (event.getKeyCode() == KeyEvent.VK_DOWN) {//先 发送历史内容
			if(msgnum<msgOldList.size()) {
				msgnum = msgnum+1;
			}
			if(msgnum<msgOldList.size() && msgnum>=0) {
				ZhuFrame.getZhuJpanel().getSendMes().setText(msgOldList.get(msgnum));
			}
		} else if (event.isAltDown()) {
			try {
				//战斗状态判断
				if (FightingMixDeal.State != HandleState.USUAL) {
					// 普通攻击 alt+a
					if (event.getKeyCode() == KeyEvent.VK_A) {
						FightOperation operation = FightingMonitor.getOperation();
						operation.Record(-1, -1, 0, null);
						FightingMonitor.execution(operation);
					} else if (event.getKeyCode() == KeyEvent.VK_W) {// 弹出法术列表 alt+s
						if (FightingMixDeal.State == HandleState.HANDLE_PLAYER|| FightingMixDeal.State == HandleState.HANDLE_PET) {
							FightingBtn.Btnfashu();
						}
					} else if (event.getKeyCode() == KeyEvent.VK_D) {// 防御alt+d
						if (FightingMixDeal.State == HandleState.HANDLE_PLAYER|| FightingMixDeal.State == HandleState.HANDLE_PET) {
							FightingBtn.Btnfangyu();
						}
					} else if (event.getKeyCode() == KeyEvent.VK_E) {// 物品栏alt+e
						if (!FormsManagement.getframe(2).isVisible()) {
							FormsManagement.showForm(2);
						} else {
							FormsManagement.HideForm(2);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_T) {// ALT+T 保护
						FightingBtn.Btnbaohu();
					} else if (event.getKeyCode() == KeyEvent.VK_B) {// ALT+B 捕捉
						FightingBtn.Btnbuzhua();
					} else if (event.getKeyCode() == KeyEvent.VK_S) {// ALT+s 自动法术
						if (FightingMixDeal.State == HandleState.HANDLE_PLAYER|| FightingMixDeal.State == HandleState.HANDLE_PET) {
							FightOperation operation = FightingMonitor.getOperation();
							if (operation.getSpell() == null) {
								ZhuFrame.getZhuJpanel().addPrompt2("没有默认的法术");
								return;
							}
							FightingMonitor.mousesname = operation.getSpell();
							FightingMonitor.mousestate = 1;
							MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE8);
							ZhuFrame.getZhuJpanel().HideBeastBtn();
						}
					}
				} else {
					// alt+1键打开小地图
					if (event.getKeyCode() == KeyEvent.VK_1) {
						if (!TestsmallmapJframe.getTestsmallmapJframe().isVisible()) {
							if (Util.mapmodel.getMin_x() > 0) {
								TestsmallmapJframe.getTestsmallmapJframe().setLocation(150, 170);
								FormsManagement.showForm(22);
							}
						} else {
							FormsManagement.HideForm(22);
							FormsManagement.HideForm(633);//修复小地图关闭后依然会残留坐标----小情调备注

						}
					} else if (event.getKeyCode() == KeyEvent.VK_2) {//任我行
						if (!FormsManagement.getframe(1102).isVisible()) {
							WorldMapJframe.getWorldMapJpanel();
							FormsManagement.showForm(1102);
						} else {
							FormsManagement.HideForm(1102);
						}
					}
					//飞行器
					else if (event.getKeyCode() == KeyEvent.VK_3) {// 飞行器 alt+D
						AircraftBtn.quickFly();
					}
//					else if (event.getKeyCode() == KeyEvent.VK_5) {// 飞行器 alt+D
//						if(!Util.ycwj) {
//							Util.ycwj = true;
//						}else {
//							Util.ycwj = false;
//						}
//					}
					else if (event.getKeyCode()==KeyEvent.VK_C){
						if (!FormsManagement.getframe(119).isVisible()){
							String sendmes=Agreement.getAgreement().FlyAgreement();
							SendMessageUntil.toServer(sendmes);
						}else {
							FormsManagement.HideForm(119);
						}
					}

					else if (event.getKeyCode() == KeyEvent.VK_Q) {// alt+q 打开或者关闭任务列表
						if (!FormsManagement.getframe(3).isVisible()) {
							FormsManagement.showForm(3);
							TesttaskJframe.getTesttaskJframe().getJtask().showTaskMethod();
						} else {
							FormsManagement.HideForm(3);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_W) {// alt+w 人物属性面板
						if (!FormsManagement.getframe(0).isVisible()) {
							FormsManagement.showForm(0);
						} else {
							FormsManagement.HideForm(0);
						}
						PetAddPointMouslisten.getplayerValue();// 设置面板里面的基本参数
					} else if (event.getKeyCode() == KeyEvent.VK_O) {// 召唤兽属性面板
						// 将召唤兽信息放入到列表中
						List<RoleSummoning> pets = UserMessUntil.getPetListTable();
						TestPetJpanel.showListModel(pets, RoleData.getRoleData().getLoginResult().getSummoning_id());
//						for (int i = 0; i < 4; i++) {
//							TestPetJpanel.getLabNedan()[i].setGoodstable(null);
//							TestPetJframe.getTestPetJframe().getTestPetJpanel().remove(TestPetJpanel.getLabNedan()[i]);
//						}
						// 如果已经携带了召唤兽,那么打开面板的时候默认就打开所有的选中的信息
						if (pets.size() != 0) {
							UserMessUntil.setChosePetMes( UserMessUntil.getPetRgid(RoleData.getRoleData().getLoginResult().getSummoning_id()));
							PetPanelShow.ShowMesForJpanel();
						}
						if (!FormsManagement.getframe(6).isVisible()) {
							FormsManagement.showForm(6);
							Music.addyinxiao("开关窗口.mp3");// 打开面板
						} else {
							FormsManagement.HideForm(6);
							Music.addyinxiao("关闭窗口.mp3");
						}
					} else if (event.getKeyCode() == KeyEvent.VK_I|| event.getKeyCode() == KeyEvent.VK_E) {// 道具栏
						if (!FormsManagement.getframe(2).isVisible()) {
							// 设置存款
							ZhuJpanel.setUseGoodsType(0);
							FormsManagement.showForm(2);
						} else {
							// 将使用物品的状态改为人物点击状态
							ZhuJpanel.setUseGoodsType(0);
							FormsManagement.HideForm(2);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_A) {// 组队alt+t
						MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE6);
					}else if (event.getKeyCode() == KeyEvent.VK_Z) {// 组队alt+t
						ChosePetSkillsMouslisten.refreshPetSkills();
						if (!FormsManagement.getframe(18).isVisible()) {
							// 设置存款
							FormsManagement.showForm(18);
						} else {
							FormsManagement.HideForm(18);
						}

					} else if (event.getKeyCode() == KeyEvent.VK_T) {// 组队alt+t
						ChangeMouseSymbolMouslisten.dianji("组队快捷键");
					} else if (event.getKeyCode() == KeyEvent.VK_G) {// 给予alt+g
						MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE3);
					} else if (event.getKeyCode() == KeyEvent.VK_X) {// 交易alt+x
						MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE5);
					} else if (event.getKeyCode() == KeyEvent.VK_R) {// 坐骑alt+r
						if (!FormsManagement.getframe(7).isVisible()) {
							// 向服务器发送消息请求坐骑列表
							FormsManagement.showForm(7);
							String sendmes = Agreement.getAgreement().MountAgreement();
							SendMessageUntil.toServer(sendmes);
						} else {
							FormsManagement.HideForm(7);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_F) {// 好友alt+f
						MessagrFlagUntil.ReceiveFriend();
					} else if (event.getKeyCode() == KeyEvent.VK_Y) {// 宝宝alt+y
						if (!FormsManagement.getframe(1).isVisible()) {
							Baby baby = UserMessUntil.getbaby(TestChildJframe.getTestChildJframe().getTestChildJpanel().getBabyid());
							TestChildJframe.getTestChildJframe().getTestChildJpanel().ShowBaby(baby);
							FormsManagement.showForm(1);
						} else {
							FormsManagement.HideForm(1);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_V) {// ALT+v// 法宝界面
						if (!FormsManagement.getframe(43).isVisible()) {
							FormsManagement.showForm(43);
						} else {
							FormsManagement.HideForm(43);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_B) {// ALT+b	// 帮派界面
						if (FormsManagement.getframe(48).isVisible()) {
							FormsManagement.HideForm(48);
							return;
						}
						if (ImageMixDeal.userimg.getRoleShow().getGang_id() == null|| ImageMixDeal.userimg.getRoleShow().getGang_id().intValue() == 0) {
							ZhuFrame.getZhuJpanel().addPrompt2("你没有帮派!");
							return;
						}
						// 向服务器发送信息
						String senmes = Agreement.getAgreement().IntogangAgreement(ImageMixDeal.userimg.getRoleShow().getGang_id().toString());
						SendMessageUntil.toServer(senmes);
					} else if (event.getKeyCode() == KeyEvent.VK_S) {// ALT+s 游戏设置
						if (!FormsManagement.getframe(50).isVisible()) {
							FormsManagement.showForm(50);
						} else {
							FormsManagement.HideForm(50);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_0) {// ALT+s 小表情界面
						if (!FormsManagement.getframe(52).isVisible()) {
							// 设置存款
							FormsManagement.showForm(52);
						} else {
							FormsManagement.HideForm(52);
						}
					} else if (event.getKeyCode() == KeyEvent.VK_3) {
//						TabJFrame.showGames(1);
					} else if (event.getKeyCode() == KeyEvent.VK_4) {
						ManimgAttribute.ISNAME = !ManimgAttribute.ISNAME;
					} else if (event.getKeyCode() == KeyEvent.VK_5) {
						ManimgAttribute.ISTCP = !ManimgAttribute.ISTCP;
					} else if (event.getKeyCode() == KeyEvent.VK_6) {
						ImageMixDeal.ISSTALL = !ImageMixDeal.ISSTALL;
					} else if (event.getKeyCode() == KeyEvent.VK_P) {// ALT+P 打开商城
						if (!FormsManagement.getframe(39).isVisible()) {

							FormsManagement.showForm(39);
						} else {
							FormsManagement.HideForm(39);
						}
					}else  if (event.getKeyCode() == KeyEvent.VK_TAB){

					}else if(event.getKeyCode() == KeyEvent.VK_N){
						if (!FormsManagement.getframe(3100).isVisible()) {
							XBjczJpanelxsjm.getXBjczJpanelxsjm().getXBjczJpanel();
							FormsManagement.showForm(3100);
						}else{
							FormsManagement.HideForm(3100);
						}
					}else if(event.getKeyCode() == KeyEvent.VK_J){
						if (!FormsManagement.getframe(82).isVisible()) {
							SkillMainFrame.getSkillMainFrame().getSkillMainPanel().changeBtnPanel(0);
							FormsManagement.showForm(82);
						}else {
							FormsManagement.HideForm(82);
						}
					}
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	public static String getMesageType() {
		return mesageType;
	}

	public static void setMesageType(String mesageType) {
		MyAWTEventListener.mesageType = mesageType;
	}

	/** 聊天框处理 */
	private static long time;

	public static void Mesage() {
		// 3514
		JTextField field = ZhuFrame.getZhuJpanel().getSendMes();
		if (field.isFocusOwner()) {
			String sendmes = ((RichDocument) field.getDocument()).sendText();// 发送的消息
			if (sendmes.equals("")) {
				ZhuFrame.getZhuJpanel().addPrompt("请输入内容");
				return;
			}
			int max=RoleData.getRoleData().getLoginResult().getTurnAround()*120;
			if (max<120) {max=120;}
			if (field.getText().length() > max) {
				ZhuFrame.getZhuJpanel().addPrompt("最大字符限制"+max+"个");
				return;
			}
			if (mgc(sendmes)) {
				ZhuFrame.getZhuJpanel().addPrompt("发送内容带有敏感词");
				return;
			}
			// 将聊天输入框的内容清空
			field.setText("");
			setOldMsgList(sendmes);
			Mesage(sendmes, mesageType);
		} else {
			field.requestFocus();
		}
	}

	public static void Mesage(String sendmes, String type) {
		// 0当前 1队伍 2帮派 3世界 4传音
		NChatBean bean = new NChatBean();
		bean.setMessage(sendmes);
		final LoginResult login = RoleData.getRoleData().getLoginResult();
		if (StringUtils.isNotEmpty(login.getLiangHao())) {
			bean.setGoodNum(login.getLiangHao());
			bean.setGoodNumType(login.getLianghaotype());
		}
		if (type.equals("世界")) {
			bean.setId(3);
			long time2 = System.currentTimeMillis();
			if (time2 - time < 2000) {
				ZhuFrame.getZhuJpanel().addPrompt("世界喊话间隔2秒");
				return;
			}
			time = time2;
		} else if (type.equals("当前")) {
			bean.setId(0);
			long time2 = System.currentTimeMillis();
			if (time2 - time < 1000) {
				ZhuFrame.getZhuJpanel().addPrompt("当前喊话间隔1秒");
				return;
			}
			time = time2;
		} else if (type.equals("帮派")) {
			if (ImageMixDeal.userimg.getRoleShow().getGang_id().compareTo(new BigDecimal(0)) != 1) {
				ZhuFrame.getZhuJpanel().addPrompt2("您没有帮派，无法发送帮派信息！！");
				return;
			}
			long time2 = System.currentTimeMillis();
			if (time2 - time < 1000) {
				ZhuFrame.getZhuJpanel().addPrompt("当前喊话间隔1秒");
				return;
			}
			time = time2;
			bean.setId(2);
		} else if (type.equals("队伍")) {
			if (mesageType.equals("队伍")) {
				if (ImageMixDeal.userimg.getRoleShow().getTroop_id() == null) {
					ZhuFrame.getZhuJpanel().addPrompt2("您还没有加入队伍，无法发送队伍信息！！");
					return;
				}
			}
			bean.setId(1);
		} else if (type.equals("传音")) {
			// 判断是否有小喇叭
			int a = 0;
			for (int i = 0; i < GoodsListFromServerUntil.getGoodslist().length; i++) {
				Goodstable good = GoodsListFromServerUntil.getGoodslist()[i];
				if (good == null)
					continue;
				if (good.getType() == 2324) {
					// 消耗喇叭，发送物品使用情况
					good.setUsetime(good.getUsetime() - 1);
					GoodsMouslisten.gooduse(good, 1);
					if (good.getUsetime() <= 0)
						GoodsListFromServerUntil.Deleted(i);
					a = 1;
					break;
				}
			}
			if (a == 0) {
				ZhuFrame.getZhuJpanel().addPrompt2("您没有足够小喇叭了！！！");
				return;
			}
			bean.setId(4);
		}
		String value = Agreement.getAgreement().chatAgreement(GsonUtil.getGsonUtil().getgson().toJson(bean));
		// 向服务器发送信息
		SendMessageUntil.toServer(value);
	}

	static String[] v = { "#T", "#X", "#J", "#Q", "#D", "#P", "Q号", "q号", "QQ","qq", "Qq", "公益服", "免费送", "扣扣群", "裙","GM","测试","福利" };

	// 敏感词判断
	public static boolean mgc(String text) {
		for (int i = 0; i < v.length; i++) {
			if (text.indexOf(v[i]) != -1) {
				return true;
			}
		}
		return false;
	}

	//存储消息发送历史
	public static void setOldMsgList(String msg) {
		if(!setContains(msgOldList, msg)) {
			if(msgOldList.size()>50) {
				msgOldList.remove(0);
			}
			msgOldList.add(msg);
			msgnum = msgOldList.size();
		}
	}
	public static boolean setContains(List<String> list, String value) {
		Set<String> stringSet = new HashSet<String>(list);
		return stringSet.contains(value);
	}

}